package net.blank.broadcast
{
	import flash.utils.Dictionary;
	
	import net.blank.functionUtl.ExecFunctions;

	/**
	 * 广播
	 * @author blank
	 * 
	 */
	public final class Broadcast
	{
		/**
		 * 收听者列表
		 */
		private static var listeners:Dictionary=new Dictionary();
		
		/**
		 * 添加收听者
		 * @param channel 频道
		 * @param listener 收听者
		 * @return 
		 * 
		 */
		public static function addListener(channel:String,listener:Function):Boolean{
			var exec:ExecFunctions=listeners[channel];
			if(!exec){
				exec=listeners[channel]=new ExecFunctions();
			}
			return exec.addFunc(listener);
		}
		
		/**
		 * 移除收听者
		 * @param channel 频道
		 * @param listener 收听者
		 * 
		 */
		public static function removeListener(channel:String,listener:Function):void{
			var exec:ExecFunctions=listeners[channel];
			if(exec){
				exec.removeFunc(listener);
				if(!exec.numFunc){
					delete listeners[channel];
				}
			}
		}
		
		/**
		 * 移除频道下的所有收听者
		 * @param channel 频道
		 * 
		 */
		public static function removeChannel(channel:String):void{
			delete listeners[channel];
		}
		
		/**
		 * 移除所有收听者
		 * 
		 */
		public static function removeAll():void{
			listeners=new Dictionary();
		}
		
		/**
		 * 发送广播信息
		 * @param channel 频道
		 * @param args 参数
		 * 
		 */
		public static function sendMessage(channel:String,args:Array=null):void{
			var exec:ExecFunctions=listeners[channel];
			if(exec){
				exec.exec_appointedArgs(args);
			}
		}
	}
}